package com.micro.school.h5.util;

import com.alibaba.fastjson2.JSON;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;

public class HttpUtil {

    public static String sendPostRequest(Map params, String url) throws URISyntaxException, IOException, InterruptedException {
        // 目标URL
        URI RequestUrl = new URI(url);
        HttpRequest request = HttpRequest.newBuilder()
                .uri(RequestUrl)
                .headers("Content-Type", "application/json")
                .headers("HH-Client-ID", "ygx2a3d8d7379323ba33e645fdfddygx")
                .POST(HttpRequest.BodyPublishers.ofString(JSON.toJSONString(params,"UTF-8")))
                .build();
        HttpClient client = HttpClient.newHttpClient();
        // 发送请求并接收响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        return response.body();
    }

}
